﻿using System.Collections.Generic;
using System.Web.Mvc;
using Dexter.Dependency;
using Dexter.Domain.Model;
using Dexter.Services;
using Dexter.Web.Mvc.ResourceCombiner;
using Dexter.Web.Utils;

namespace Dexter.Web.Mvc.ViewPages {
	public abstract class RazorViewPage <TModel> : RazorView , IDexterView where TModel : class {
		readonly ControllerContext contex;
		ICombiner combiner;
		SiteConfiguration siteConfiguration;
		IUrlBuilder urlBuilder;
		IWebUtils utils;

		/// <summary>
		/// 	Initializes a new instance of the <see cref = "RazorViewPage&lt;TModel&gt;" /> class.
		/// </summary>
		/// <param name = "controllerContext">The controller context.</param>
		/// <param name = "viewPath">The view path.</param>
		/// <param name = "layoutPath">The layout path.</param>
		/// <param name = "runViewStartPages">if set to <c>true</c> [run view start pages].</param>
		/// <param name = "viewStartFileExtensions">The view start file extensions.</param>
		protected RazorViewPage ( ControllerContext controllerContext , string viewPath , string layoutPath , bool runViewStartPages , IEnumerable <string> viewStartFileExtensions ) : base ( controllerContext , viewPath , layoutPath , runViewStartPages , viewStartFileExtensions ) {
			contex = controllerContext;
			CreateHelpers ( );
		}

		/// <summary>
		/// 	Initializes a new instance of the <see cref = "RazorViewPage&lt;TModel&gt;" /> class.
		/// </summary>
		/// <param name = "controllerContext">The controller context.</param>
		/// <param name = "viewPath">The view path.</param>
		/// <param name = "layoutPath">The layout or master page.</param>
		/// <param name = "runViewStartPages">A value that indicates whether view start files should be executed before the view.</param>
		/// <param name = "viewStartFileExtensions">The set of extensions that will be used when looking up view start files.</param>
		/// <param name = "viewPageActivator">The view page activator.</param>
		protected RazorViewPage ( ControllerContext controllerContext , string viewPath , string layoutPath , bool runViewStartPages , IEnumerable <string> viewStartFileExtensions , IViewPageActivator viewPageActivator )
			: base ( controllerContext , viewPath , layoutPath , runViewStartPages , viewStartFileExtensions , viewPageActivator ) {
			contex = controllerContext;
			CreateHelpers ( );
		}


		#region IDexterView Members

		/// <summary>
		/// 	Return the instance of <see cref = "IUrlBuilder" />.
		/// </summary>
		/// <value>The U.</value>
		public IUrlBuilder U {
			get { return urlBuilder; }
		}

		public SiteConfiguration C {
			get { return siteConfiguration; }
		}

		/// <summary>
		/// 	Gets the combiner.
		/// </summary>
		/// <value>The combiner.</value>
		public ICombiner Combiner {
			get { return combiner; }
		}

		/// <summary>
		/// 	Gets the utils.
		/// </summary>
		/// <value>The utils.</value>
		public IWebUtils Utils {
			get { return utils; }
		}

		/// <summary>
		/// 	Gets a value indicating whether this instance is moderator.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance is moderator; otherwise, <c>false</c>.
		/// </value>
		public bool IsModerator {
			get { return this.contex.HttpContext.User.IsInRole ( "Moderator" ); }
		}

		/// <summary>
		/// 	Check if the specified username is the current logged user.
		/// </summary>
		/// <param name = "username">The username.</param>
		/// <returns></returns>
		public bool CurrentuserIsEqualtTo ( string username ) {
			return this.contex.HttpContext.User.Identity.Name == username;
		}

		#endregion

		/// <summary>
		/// 	Creates the helpers.
		/// </summary>
		protected void CreateHelpers ( ) {
			urlBuilder = DexterContainer.Resolve <IUrlBuilder> ( );
			siteConfiguration = DexterContainer.Resolve <IConfigurationService> ( ).Configuration;
			combiner = DexterContainer.Resolve <ICombiner> ( );
			utils = DexterContainer.Resolve <IWebUtils> ( );
		}
	}
}